$(function () {
    getcollect(0);
});

function getcollect(page) {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var url = "/except/shopcontroller/mycollect.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            page: page,
            json: jsonstr,
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var addressinfo = "";
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var collect = "";
                var dtats = data.data;
                $(dtats).each(function () {
                    // collect += '<div class="item">' +
                    //     '<div class="img_div" >' +
                    //     // '<img src="story_logo" onerror="imgerror(this,\'img/onerror.png\');"/>' +
                    //     '<div style="float: left;margin-right: 10px; width:85px; height:85px;overflow-y: hidden; ' +
                    //     'background: url('+this.img+') no-repeat center;background-size: cover;"' +
                    //     'onerror="imgerror(this,\'img/onerror.png\');"></div>'+
                    //
                    //     '</div>' +
                    //     '<div class="center_div" >' +
                    //     '<ul>' +
                    //     '<li class="title">' + this.story_name + '</li>' +
                    //     '<li style="margin-top: 5px;"><span class="desc">' + this.brief + '</span></li>' +
                    //     '</ul>' +
                    //     '</div>' +
                    //     '<div style="float: right;">' +
                    //     '<span class="right">取消收藏</span>' +
                    //     '</div>' +
                    //     '<div class="clearboth"></div>' +
                    //     '</div>';
                    var storytype = this.type;
                    var locationurl = "";
                    var storyid = this.story_id;
                    var shopid = this.shopid;
                    if (storytype == "1")
                        locationurl = "/except/files/shop_story.jsp?storyid=" + storyid + "&shopid=" + shopid;
                    else
                        locationurl = "/except/files/story.jsp?storyid=" + storyid;
                    collect += '<div class="item clearboth" id="collectstory' + this.story_id + '">' +
                        '<div class="img_div" onclick="location.href=\'' + locationurl + '\'">' +
                        '<img src="' + this.story_logo + '"  onerror="imgerror(this,\'img/onerror.png\');"/>' +
                        '</div>' +
                        '<div class="center_div">' +
                        '<ul>' +
                        '<li>' +
                        '<span>' + this.story_name + '</span>' +
                        '<span class="right" onclick="canclecollect(\'' + this.story_id + '\')">取消收藏</span>' +
                        '</li>' +
                        '<li><span class="desc">' + this.brief + '</span></li>' +
                        '</ul>' +
                        '</div>' +
                        '</div>';


                });
                remove_advertising()
            } else if (code == 20013) {
                if (page == 0) {
                    $("#not_data").show();
                }
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            if (page == 0)
                $("#collect").empty().append(collect);
            else
                $("#collect").append(collect);
            $("#page").val(page * 1 + 1);
        }
    });
}

function canclecollect(storyid) {
    $("#message").html("确定删除吗？");
    $("#confirmbg").show();
    $("#storyid").val(storyid);
}

function confirm_state(state) {
    $("#confirmbg").hide();
    var storyid = $("#storyid").val();
    if (state == 0) {
    } else {
        var token = $("#token").val();
        if (isempty(token)) {
            location.href = "/except/login.jsp";
            return;
        }
        var jsonstr = "{token:" + token + "}";
        var url = "/except/shopcontroller/delmycollect.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                storyid: storyid,
                json: jsonstr,
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                var addressinfo = "";
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#hint_message").html("已取消");
                    $("#hintbg").show();
                    $("#collectstory" + storyid).remove();
                    var i = 0;
                    $("#collect .item").each(function () {
                        i++;
                    });
                    if (i <= 0)
                        $("#not_data").show();
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    }
}

$(window).scroll(function () {
    if ($(document).scrollTop() <= 0) {
        getcollect(0);
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        var page = $("#page").val();
        getcollect(page);
    }
});